﻿<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <!--全局通用框架样式 begin-->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--/*@thymesVar id="title" type=""*/-->
    <title>汽车信息</title>
    <link href="/coral/images/favicon.ico" rel="icon">
    <link href="/assets/libs/layui/css/layui.css" rel="stylesheet"/>
    <link href="/assets/module/admin.css" rel="stylesheet">

    <link href="//cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/assets/js/common.js"></script>
    <script type="text/javascript" src="/coral/js/custom.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                    <label class="layui-form-label layui-form-required">车牌号:</label>
                    <div class="layui-input-block">
                        <!--                <input name="carId" placeholder="请输入汽车id" class="layui-input"-->
                        <!--                       lay-verType="tips" lay-verify="required" required/>-->
                        <select name="carId"></select>
                    </div>
                    </div>
                </div>
                <!--                <div class="layui-form-item">-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">汽车id:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="carId" class="layui-input" placeholder="请输入汽车id"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">用户id:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="userId" class="layui-input" placeholder="请输入用户id"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">用户名:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="userName" class="layui-input" placeholder="请输入用户名"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">耗时:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="minutes" class="layui-input" placeholder="请输入耗时"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">费用:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="money" class="layui-input" placeholder="请输入费用"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                                    <div class="layui-inline">
                                        <label class="layui-form-label">状态:</label>
                                        <div class="layui-input-inline">
                                            <input name="state" class="layui-input" placeholder="请输入状态"/>
                                        </div>
                                    </div>
                <div class="layui-inline">&emsp;
                    <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>&nbsp;
                    <button shiro:hasPermission="usageRecord:export" id="exportBtn" class="layui-btn icon-btn" type="button">
                        <i class="layui-icon">&#xe67d;</i>导出
                    </button>
                   </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
<!--        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">所属公司:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input name="company" placeholder="请输入所属公司" class="layui-input"-->
<!--                       lay-verType="tips" lay-verify="required" required/>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">汽车:</label>-->
<!--            <div class="layui-input-block">-->
<!--                &lt;!&ndash;                <input name="carId" placeholder="请输入汽车id" class="layui-input"&ndash;&gt;-->
<!--                &lt;!&ndash;                       lay-verType="tips" lay-verify="required" required/>&ndash;&gt;-->
<!--                <select name="carId" id="carId"></select>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">用户:</label>-->
<!--            <div class="layui-input-block">-->
<!--                &lt;!&ndash;                <input name="userId" placeholder="请输入用户id" class="layui-input"&ndash;&gt;-->
<!--                &lt;!&ndash;                       lay-verType="tips" lay-verify="required" required/>&ndash;&gt;-->
<!--                <select name="userId" lay-filter="userId"></select>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">用户名:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input name="userName" placeholder="请输入用户名" class="layui-input"-->
<!--                       lay-verType="tips" lay-verify="required" required/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">耗时:</label>
            <div class="layui-input-block">
                <input name="minutes" placeholder="请输入耗时" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">费用:</label>
            <div class="layui-input-block">
                <input name="money" placeholder="请输入费用" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">事由:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input name="reason" placeholder="请输入使用事由" class="layui-input"-->
<!--                       lay-verType="tips" lay-verify="required" required/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">起始时间:</label>
            <div class="layui-input-block">
                <input id="creDate" name="creDate" placeholder="请选择起始时间"
                       class="layui-input icon-date" autocomplete="off" lay-verType="tips"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">结束时间:</label>
            <div class="layui-input-block">
                <input id="endDate" name="endDate" placeholder="请选择结束时间"
                       class="layui-input icon-date" autocomplete="off" lay-verType="tips"
                       lay-verify="required" required/>
            </div>
        </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">状态:</label>
                    <div class="layui-input-block">
                        <select name="states" placeholder="请输入状态" class="layui-input"
                                lay-verType="tips" lay-verify="required" required>
                            <option value="-1">请选择</option>
                            <option value="0">正在使用</option>
                            <option value="1">结束使用</option>
                        </select>
                    </div>
                </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'company', title: "所属公司", align: 'center'},
            {
                field: 'carId', title: '汽车', align: 'center', templet: function (d) {
                    var name = "";
                    $.ajax({
                        url: "/car/selCarById?id=" + d.carId,
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {
                            if (res.body) {
                                name = res.body.carNumber;
                                ;
                            }
                        }
                    })
                    return name;
                }
            },
            {
                field: 'userId', title: '用户编号', align: 'center'
            },
            {field: 'userName', title: '用户名', align: 'center'},
            {field: 'minutes', title: '耗时', align: 'center'},
            {field: 'reason', title: '事由', align: 'center'},
            {field: 'money', title: '费用', align: 'center'},
            {
                field: 'creDate', title: '使用时间', align: 'center', templet: function (d) {
                    if (d.endDate) {
                        return d.creDate + " - " + d.endDate;
                    } else {
                        return d.creDate + " - 至今";
                    }
                }
            },
            {field: 'states', title: '状态', align: 'center',templet:function (d) {
                    var s=["正在使用","结束使用"];
                    return s[d.states];
                }},
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ];
        $.ajax({
            url: "/car/selCarList",
            type: 'post',
            dataType: 'JSON',
            contentType: 'application/json',
            async: false,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("X-Token", C.find("X-Token"));
            },
            success: function (res) {

                var data = res.body;
                if (data) {
                    var html = "<option value=''>请选择车辆</option>";
                    for (var i = 0; i < data.length; i++) {
                        var tp = data[i];

                                html += "<option value='" + tp.id + "'>" + tp.carNumber + "</option>"
                    }
                    $("[name='carId']").html("");
                    $("[name='carId']").append(html);
                    // $("#shoolId");
                    form.render('select');
                }
            }
        })
        var insTb = table.render({
            elem: '#dataTable',
            url: '/usageRecord/selUsageRecordPageAfter',
            page: true,
            toolbar: [shiroToolbar(true, false)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            method: 'post',
            dataType: 'json',
            contentType: 'application/json',
            headers: {"X-Token": C.find("X-Token")},
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.errorCode, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.body.total, //解析数据长度
                    "data": res.body.records //解析数据列表
                };
            },
            request: {
                pageName: 'pageIndex' //页码的参数名称，默认：page
                ,
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(false, true));
            }
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        // 显示编辑弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose: true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mData);  // 回显数据

                    $.ajax({
                        url: "/car/selCarList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择车辆</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.carId == tp.id) {
                                            html += "<option value='" + tp.id + "'  selected='selected'>" + tp.carNumber + "</option>"
                                        } else {
                                            html += "<option value='" + tp.id + "'>" + tp.carNumber + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.id + "'>" + tp.carNumber + "</option>"
                                    }
                                }
                                $("#carId").html("");
                                $("#carId").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })


                    $.ajax({
                        url: "/users/selUsersList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择用户</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.userId == tp.id) {
                                            html += "<option value='" + tp.id + "'  selected='selected'>" + tp.name + "</option>"
                                        } else {
                                            html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                    }
                                }
                                $("[name='userId']").html("");
                                $("[name='userId']").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })

                    form.on('select(userId)', function (data) {
                        if (data.value) {
                            $.ajax({
                                url: "/users/selUsersById?id=" + data.value,
                                type: 'post',
                                dataType: 'JSON',
                                contentType: 'application/json',
                                async: false,
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.body) {
                                       var  name = res.body.name;
                                        $("[name='userName']").val(name);
                                    }
                                }
                            })
                        }
                    })

                    form.on('submit(saveBtn)', function (data) {
                        var carId = $("#carId").val();
                        var company = $("[name='company']").val();
                        var userId = $("[name='userId']").val();
                        var userName = $("[name='userName']").val();
                        var minutes = $("[name='minutes']").val();
                        var money = $("[name='money']").val();
                        var creDate = $("[name='creDate']").val();
                        var endDate = $("[name='endDate']").val();
                        var reason = $("[name='reason']").val();
                        var states = $("[name='states']").val();

                        if (mData) {
                            var data = {
                                "userId": userId,
                                "id": mData.id,
                                "carId": carId,
                                "userName": userName,
                                "minutes": minutes,
                                "money": money,
                                "creDate": creDate,
                                "endDate": endDate,
                                "states": states
                            }
                            $.ajax({
                                url: '/usageRecord/update',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('修改成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        } else {
                        var data = {
                            "userId": userId,
                            "company": company,
                            "carId": carId,
                            "userName": userName,
                            "minutes": minutes,
                            "money": money,
                            "creDate": creDate,
                            "endDate": endDate,
                            "reason": reason
                        }

                        $.ajax({
                            url: '/usageRecord/insertBegin',
                            type: 'post',
                            data: JSON.stringify(data),
                            dataType: 'JSON',
                            contentType: 'application/json',
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("X-Token", C.find("X-Token"));
                            },
                            success: function (res) {
                                if (res.message == 'success') {
                                    layer.msg('添加成功', {icon: 1})
                                    layer.close(dIndex);
                                    // show();
                                    insTb.reload({page: {curr: 1}});
                                    ;
                                } else {
                                    layer.msg(res.message);
                                }
                                //     if (res.code == 0) {
                                //     layer.close(dIndex);
                                //     layer.msg(res.msg, {icon: 1});
                                //     insTb.reload({page: {curr: 1}});
                                // } else {
                                //     layer.msg(res.msg, {icon: 2});
                                // }
                            }
                        })
                        }

                        return false;
                    });
                }
            });

            /* 渲染laydate */
            laydate.render({
                elem: '#creDate',
                type: 'datetime'
            });
            /* 渲染laydate */
            laydate.render({
                elem: '#endDate',
                type: 'datetime'
            });
        }

        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            } else {
                data.field.startDate = '';
                data.field.endDate = '';
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });


        /* 删除 */
        window.doDel = function (obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('usageRecord/delete', {
                    id: obj ? obj.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
    });
</script>
</body>
</html>